<?php
class JiekouApp extends ApiApp
{
    function index()
    {
		//获取接口传递过来的参数
		$style = "json";//目前只支持json格式
		$func = $_POST['func'] ? dowith_sqls($_REQUEST['func']) : "trace";//操作类型
		$partner = dowith_sqls($_REQUEST['partner']); 
		$pass = dowith_sqls($_REQUEST['pass']); 
		$content = dowith_sqls($_REQUEST['content']);
		$verify = dowith_sqls($_REQUEST['verify']);
		$result = array();//内容
		$status = "OK";//OK是对的，NO是参数有误 *
		//判断是否过来了
		if(!$style || !$func || !$partner || !$content || !$verify)
		{
			$status = "NO";
			$code = "s05";
			$remark = urlencode('缺少完整的参数');
			$this->output_json(NULL, NULL, $verify, $status, $code, $remark);	
		}
		
		//判断操作类型
		if($func != "trace")
		{
			$status = "NO";
			$code = "s03";
			$remark = urlencode('无效的指令操作');
			$this->output_json(NULL, NULL, $verify, $status, $code, $remark);	
		}
		
		//开始判断有效时间
		//$contrast_time = time() - strtotime($datetime);
		//if( $contrast_time < -120 || $contrast_time > 1800) //30分钟1800秒
		//{
		//	$result[] = array('acceptTime' => date("Y-m-d H:i:s",time()), 'remark' => urlencode('不是有效日期，相差了'.$contrast_time.'秒'));
		//	$status = "NO";
		//	$this->output_json($yundanhao, $result, $verify, $status, $code, $remark);
		//}
		
		//判断是否有效的帐号密码
		$kd_kehu_mod =& m('kd_kehu');		
		$kehu_data = $kd_kehu_mod->getRow("SELECT status,partner,pass FROM {$kd_kehu_mod->table} WHERE partner='{$partner}' ");			
		if(!$kehu_data){
			$status = "NO";
			$code = "s08";
			$remark = urlencode('非法的账户信息');
			$this->output_json(NULL, NULL, $verify, $status, $code, $remark);	
		}
		else
		{
			if($kehu_data['pass'] == '1'){
				$status = "NO";
				$code = "s53";
				$remark = urlencode('已暂停用户的权限');
				$this->output_json(NULL, NULL, $verify, $status, $code, $remark);	
			}	
			//判断请求的校验加密串
			$now_verify = md5($partner.$content.$kehu_data['pass']);
			if($verify != $now_verify){
				$status = "NO";
				$code = "s04";
				$remark = urlencode('非法的数据签名');
				$this->output_json(NULL, NULL, $verify, $status, $code, $remark);	
			}			
		}
			
		//调用快递单号的物流信息
		switch($func)
		{
		  case 'trace':
		  
			  //解码查询单号//base64_decode($content)
			  $yundanhao = base64_decode($content); 
			  $mod_kd_order =& m('kd_order');
			  $yundan_list = $mod_kd_order->findAll(array(
				  'fields'        => 'this.yundanhao,this.express,this.expressNo,this.addtime',
				  'conditions'    => 'yundanhao='.$yundanhao.'',
				  'limit'         => '0,1', 
				  'include'       =>  array(
					 'has_kdwuliu', //取出物流信息
				   ),	
			  ));
			  //print_r($yundan_list);
			  
			  if($yundan_list)
			  {
				  $yundan_list = current($yundan_list);
				  if($yundan_list['wuliu'])
				  {
					  foreach ($yundan_list['wuliu'] as $key => $row){
						  $wuliu_data[] = array('acceptTime' => date("Y-m-d H:i:s",$row['addtime']), 'remark' => urlencode($row['yudan_text']));
					  }	
				  }
				  if($wuliu_data){
					  $result['name'] = "traces";
					  $result['data'] = $wuliu_data;
					  $status = "OK";
				  }else{
					  $status = "OK";
					  $result['name'] = "traces";
					  $result['data'][0] = array( 'acceptTime' => date('Y-m-d H:i:s',$yundan_list['addtime']), 'remark' => urlencode('已录入运单'));
				  }
				  $express = $yundan_list['express'];
				  $expressNo = $yundan_list['expressNo'];
			  }
			  else
			  {
				  $status = "NO";
				  $code = "s52";
				  $remark = urlencode('单号不存在');		  
				  $result['name'] = "traces";
				  $result['data'] = urlencode('暂无物流信息');
				  $expressNo = '';	
				  $express = '';
			  }
			  
			  break; 		  
		  default:
		}
		$this->output_json($yundanhao,$express,$expressNo, $result, $verify, $status, $code, $remark);
    }
	/** 输出json数据****/
	function output_json($content = NULL,$express = NULL,$expressNo = NULL, $result = NULL, $verify, $status, $code, $remark)
	{
		!$result && $status = 'NO';
		$array['content'] = $content;
		//暂定为接入中通的物流，一起发给客户
		if($status == 'OK' && is_array($result['data']))
		{
			require_once '../data/jiekou/interface.php';
			$expressNo && $get_array = get_wuliu($express, $expressNo, 'return');
			
			if($get_array['result']){
				$result_decode = json_decode($get_array['result'],true);
				if(is_array($result_decode['traces'])){
				  foreach($result_decode['traces'] as $key => $lists){
					  $result['data'][] = array('acceptTime' => $lists['acceptTime'], 'remark' => urlencode($lists['remark']));
				  }
				}
			}
		}
		$result && $array[$result['name']] = $result['data'];
		$code && $array['code'] = $code;
		$remark && $array['remark'] = $remark;
		$array['verify'] = $verify;
		$array['status'] = $status;
		$json_txt = json_encode($array);
		$return_txt = urldecode($json_txt);//结合urlencode防止json_encode把中文转码
		//输出内容==============================
		exit($return_txt);	
	}		
} 
function dowith_sqls($str)
{
	$str = str_replace("and","",$str);
	$str = str_replace("execute","",$str);
	$str = str_replace("update","",$str);
	$str = str_replace("count","",$str);
	$str = str_replace("chr","",$str);
	$str = str_replace("mid","",$str);
	$str = str_replace("master","",$str);
	$str = str_replace("truncate","",$str);
	$str = str_replace("char","",$str);
	$str = str_replace("declare","",$str);
	$str = str_replace("select","",$str);
	$str = str_replace("create","",$str);
	$str = str_replace("delete","",$str);
	$str = str_replace("insert","",$str);
	$str = str_replace("'","",$str);
	return $str;
}
?>